package com.sm42.baseController;

import com.sm42.baseService.BaseCrmpService;
import com.sm42.common.http.AxiosResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 1. @Description:
 * 2. @Author: Tombear
 * 3. @Time: 2022/7/15 9:56
 */
public class BaseCrmpController<T> {

    @Autowired
    private BaseCrmpService<T> baseCrmpService;

    public AxiosResult toAxios(int row){
        return row > 0 ? AxiosResult.success() : AxiosResult.error();
    }
    /**
     * 查询所有
     */
    @GetMapping("findAll")
    public List<T> findAll() {
        return baseCrmpService.findAll();
    }

    /**
     * 根据id查询一条数据
     */
    @GetMapping("find")
    public AxiosResult<T> findById(Long id) {
        return AxiosResult.success(baseCrmpService.findById(id));
    }

    /**
     * 添加品牌数据
     */
    @PostMapping("add")
    public AxiosResult add(@RequestBody T t) {
        return toAxios(baseCrmpService.add(t));
    }

    /**
     * 修改品牌数据
     */
    @PostMapping("update")
    public AxiosResult update(@RequestBody T t) {
        return toAxios(baseCrmpService.update(t));
    }

    /**
     * 删除一条数据
     */
    @GetMapping("delete")
    public AxiosResult deleteById(Long id) {
        return toAxios(baseCrmpService.deleteById(id));
    }

    /**
     * 删除多条数据
     */
    @PostMapping("deletemul")
    public AxiosResult batchDeleteByIds(@RequestBody List<Long> ids) {
        return toAxios(baseCrmpService.batchDeleteByIds(ids));
    }

}
